package com.neusoft.elmboot.service.Impl;

import java.util.ArrayList;
import java.util.List;

import com.neusoft.elmboot.mapper.*;
import com.neusoft.elmboot.po.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.neusoft.elmboot.service.OrdersService;
import com.neusoft.elmboot.utils.CommonUtil;

/**
 * 订单实现
 * Service
 */
@Service
@Slf4j
public class OrdersServiceImpl implements OrdersService{

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderDetailetMapper orderDetailetMapper;
    @Autowired
    private PointMapper pointMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EnvelopeMapper envelopeMapper;
    @Autowired
    private FoodMapper foodMapper;

    @Override
    @Transactional
    public int createOrders(Orders orders) {
        //1、查询当前用户购物车中当前商家的所有食品
        Cart cart = new Cart();
        cart.setUserId(orders.getUserId());
        cart.setBusinessId(orders.getBusinessId());
        List<Cart> cartList = cartMapper.listCart(cart);

        //2、创建订单（返回生成的订单编号）
        orders.setOrderDate(CommonUtil.getCurrentDate());
        log.info("1.创建订单");
        //默认支付状态为未支付
        orders.setOrderState(0);
        orders.setDelTag(0);

        Double d = 0.0d;
        for(Cart c : cartList) {
            Food f = foodMapper.getFoodById(c.getFoodId());
            d+=f.getFoodPrice();
        }
        if(orders.getType()==1){
            d = d*100;
            d = d*0.9;
            d = d/100;
        }
        else if(orders.getType()==2){
            d = d*100;
            d = d*0.8;
            d = d/100;
        }
        if(orders.getEnvelopeId()!=null){
            d -= envelopeMapper.getEnvelopeAmountById(orders.getEnvelopeId());
        }
        orders.setOrderTotal(d);
        log.info("订单总价为：{}",orders.getOrderTotal());
        ordersMapper.saveOrders(orders);
        int orderId = orders.getOrderId();

        //3、批量添加订单明细
        List<OrderDetailet> list = new ArrayList<>();
        for(Cart c : cartList) {
            OrderDetailet od = new OrderDetailet();
            od.setOrderId(orderId);
            od.setFoodId(c.getFoodId());
            od.setQuantity(c.getQuantity());
            list.add(od);
        }
        orderDetailetMapper.saveOrderDetailetBatch(list);
        log.info("2.添加订单明细");

        //4、从购物车表中删除相关食品信息
        cartMapper.removeCart(cart);
        log.info("3.删除对应购物车");

        //5.暂时扣下花费的积分
        Double needPoint = 5.00d * orders.getType();
        Double nowPoint = pointMapper.showPointByUserId(orders.getUserId());
        Double result = nowPoint-needPoint;
        userMapper.updatePoint(orders.getUserId(),result);
        //6.暂时扣下红包
        if(orders.getEnvelopeId()!=null) {
            Integer eid = orders.getEnvelopeId();
            envelopeMapper.deEnvelopeByEnvelopeId(eid);
        }
        log.info("4.修改红包和积分信息");
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        return ordersMapper.getOrdersById(orderId);
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId){
        return ordersMapper.listOrdersByUserId(userId);
    }

    /**
     * 完成支付
     * @param orderId
     */
    @Override
    public void updateOrderState(Integer orderId) {
        //1.获取积分使用情况
        Orders orders = ordersMapper.getOrdersById(orderId);
        //2.更新订单状态
        ordersMapper.updateOrderState(orders.getOrderId());
        log.info("1.更新订单状态");
        //3.更新用户积分（新增此次消费带来的积分）
        Double cpoint = pointMapper.showPointByUserId(orders.getUserId());
        Double result = cpoint + orders.getOrderTotal()/10.0;
        userMapper.updatePoint(orders.getUserId(),result);
        log.info("2.更新积分");
        //4.删除使用的红包
        Integer eid = orders.getEnvelopeId();
        log.info("3.删除被使用的红包：{}",eid);
        envelopeMapper.deleteEnvelopeById(eid);
    }

    /**
     * 取消订单
     * @param orderId
     */
    @Override
    public void deleteOrder(Integer orderId) {
        //1.返还扣押的积分
        Orders orders = ordersMapper.getOrdersById(orderId);
        Double needPoint = 5.00d * orders.getType();
        Double result = pointMapper.showPointByUserId(orders.getUserId()) + needPoint;
        userMapper.updatePoint(orders.getUserId(),result);
        log.info("1.返还积分");
        //2.返还扣押的红包
        Integer eid = orders.getEnvelopeId();
        envelopeMapper.enEnvelopeByEnvelopeId(eid);
        log.info("2.返还红包");
        //3.删除订单信息
        ordersMapper.deleteOrder(orders);
        log.info("3.删除订单");
    }
}